package game;

import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

@SuppressWarnings("serial")
public class Screen extends JFrame {
	private JLabel[][] labels;

	private String[][] level;

	/**
	 * 
	 * @param level
	 * @param posX
	 * @param posY
	 */
	public Screen(String[][] level, int posX, int posY) {
		super("TKgame v. 1.0");
		this.level = level;
		labels = new JLabel[level.length][level.length];

		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setLocation(100, 100);
		this.setSize(500, 500);
		this.setResizable(true);
		this.setLayout(new GridLayout(20, 20, 0, 0));
		draw();
		this.setAlwaysOnTop(true);
		this.repaint();
		this.setVisible(true);


	}

	/**
	 * 
	 * @param oldX
	 * @param oldY
	 * @param newX
	 * @param newY
	 * @param playerDirection
	 */
	public void movePlayerOnScreen(Player player, int newX, int newY,
			String playerDirection) {

		labels[player.getXpos()][player.getYpos()].setIcon(ImageLoader
				.loadImage("img/gulv2.png"));

		if (playerDirection.equals("right")) {
			labels[newX][newY].setIcon(ImageLoader
					.loadImage("img/heltHoejre.png"));
		}

		if (playerDirection.equals("left")) {
			labels[newX][newY].setIcon(ImageLoader
					.loadImage("img/heltVenstre.png"));
		}

		if (playerDirection.equals("up")) {
			labels[newX][newY].setIcon(ImageLoader.loadImage("img/heltOp.png"));
		}

		if (playerDirection.equals("down")) {
			labels[newX][newY]
					.setIcon(ImageLoader.loadImage("img/heltNed.png"));
		}
	}

	/**
	 * 
	 * @param posX
	 * @param posY
	 */
	public void draw() {
		for (int j = 0; j < level.length; j++) {
			for (int i = 0; i < level[j].length; i++) {
				if (level[i][j].equalsIgnoreCase("w")) {
					// wall
					JLabel l = new JLabel(ImageLoader.loadImage("img/mur1.png"));
					l.setSize(50, 50);
					this.add(l);
					labels[i][j] = l;
				} else if (level[i][j].equalsIgnoreCase("e")) {
					// empty
					JLabel l = new JLabel(
							ImageLoader.loadImage("img/gulv2.png"));
					l.setSize(50, 50);
					this.add(l);
					labels[i][j] = l;
				}
			}
		}
		this.repaint();
		// draw hero
		// labels[posX][posY].setIcon(ImageLoader.loadImage("img/heltOp.png"));
	}

	public void drawAgain(){
		for (int j = 0; j < level.length; j++) {
			for (int i = 0; i < level[j].length; i++) {
				if (level[i][j].equalsIgnoreCase("w")) {
					labels[i][j].setIcon(new ImageIcon("img/mur1.png"));
				}
				else if (level[i][j].equalsIgnoreCase("e")) {
					labels[i][j].setIcon(new ImageIcon("img/gulv2.png"));
				}
			}
		}
	}

	public void update(ArrayList<Player> players) {
		drawAgain();
		for (Player p : players) {
			int x = p.getXpos();
			int y = p.getYpos();
			
			movePlayerOnScreen(p, x, y, p.getDirection());

//			if (p.getDirection().equals("right")) {
//				System.out.println("Draw player right "+p.getName());
//				labels[x][y].setIcon(
//						new ImageIcon("./Image/Helthoejre.png"));
//			};
//			if (p.getDirection().equals("left")) {
//				System.out.println("Draw player left "+p.getName());
//				labels[x][y].setIcon(
//						new ImageIcon("./Image/Heltvenstre.png"));
//			};
//			if (p.getDirection().equals("up")) {
//				System.out.println("Draw player up "+p.getName());
//				labels[x][y].setIcon(
//						new ImageIcon("./Image/HeltOp.png"));
//			};
//			if (p.getDirection().equals("down")) {
//				System.out.println("Draw player down "+p.getName());
//				labels[x][y].setIcon(
//						new ImageIcon("./Image/HeltNed.png"));
//			};
		}
		//		
		//		this.repaint();
	}
}
